package com.archstoday.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;

import com.archstoday.BaseApp;
import com.archstoday.R;
import com.archstoday.activity.CommentActivity;
import com.archstoday.activity.LoginActivity;
import com.archstoday.activity.TopsDetailsActivity;
import com.archstoday.adapter.CommentFromMeAdapter;
import com.archstoday.adapter.CommentToMeAdapter;
import com.archstoday.common.AsRes;
import com.archstoday.common.SpfUtils;
import com.archstoday.entity.CommentFromEntity;
import com.archstoday.entity.CommentToEntity;
import com.archstoday.utils.OAuthUtils;
import com.archstoday.utils.ToolUtils;
import com.archstoday.view.PullToRefreshLayout;
import com.base.http.common.BaseHttpClient;
import com.base.utils.MySharedPreferencesMgr;
import com.xw.repo.refresh.PullListView;

/**
 * 用户评论页面列表
 */
public class CommentFragment extends AsBaseFragment implements PullToRefreshLayout.OnRefreshListener {

    private PullToRefreshLayout mPullLayout;
    private PullListView mPullList;

    private CommentToMeAdapter mToMeAdapter;
    private CommentFromMeAdapter mFromMeAdapter;

    private int mType;
    private int page;

    public static final CommentFragment newInstance(int type) {
        CommentFragment fragment = new CommentFragment();
        Bundle bundle = new Bundle();
        bundle.putInt("type",type);
        fragment.setArguments(bundle);

        return fragment ;
    }

    @Override
    protected View initView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_comment, null);
        mPullLayout = (PullToRefreshLayout) view.findViewById(R.id.pull_layout);
        mPullList = (PullListView) view.findViewById(R.id.pull_list);
        mPullLayout.setOnRefreshListener(this);

        return view;
    }

    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);
        if (isVisibleToUser && mAct != null) {
            if (mType == 0) {
                ((CommentActivity) mAct).setTitleTv(mToMeAdapter.getCount());
            } else {
                ((CommentActivity) mAct).setTitleTv(mFromMeAdapter.getCount());
            }
        }
    }

    @Override
    protected void initData(Bundle savedInstanceState) {
        mType = getArguments().getInt("type");
        if (mType == 0) {
            mToMeAdapter = new CommentToMeAdapter(mAct);
            mPullList.setAdapter(mToMeAdapter);
            mToMeAdapter.notifyDataSetChanged();
        } else if (mType == 1) {
            mFromMeAdapter = new CommentFromMeAdapter(mAct);
            mPullList.setAdapter(mFromMeAdapter);
            mFromMeAdapter.notifyDataSetChanged();
        }
        page = 1;
        upData();
    }

    @Override
    protected void initListener() {
        mPullList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                if (mType == 1) {
                    if (mFromMeAdapter.getItem(i).getArticle().getCategoryId1() == 1) {
                        Intent intent = new Intent(mAct, TopsDetailsActivity.class);
                        intent.putExtra("id", mFromMeAdapter.getItem(i).getArticle().getArticleId());
                        intent.putExtra("isNews", true);
                        startActivity(intent);
                    }
                } else {
                    if (mToMeAdapter.getItem(i).getArticle().getCategoryId1() == 1) {
                        Intent intent = new Intent(mAct, TopsDetailsActivity.class);
                        intent.putExtra("id", mToMeAdapter.getItem(i).getArticle().getArticleId());
                        intent.putExtra("isNews", true);
                        startActivity(intent);
                    }
                }
            }
        });
    }

    private void upData() {
        switch (mType) {
            case 0:
                getToMe();
                break;
            case 1:
                getFromMe();
                break;
        }
    }

    private void getToMe() {
        initParameter();
        mParams.put("userId", MySharedPreferencesMgr.getString(SpfUtils.USER_ID, ""));
        mParams.put("token", MySharedPreferencesMgr.getString(SpfUtils.USER_TOKEN, ""));
        mParams.put("plat", "android");
        mParams.put("ifa", BaseApp.getDeviceID(mAct));
        mParams.put("page", page);
        mParams.put("pageSize", 10);

        sendEncryptPostRequest(AsRes.getInstance().getUrl(AsRes.GET_COMMENT_TO_ME),
                AsRes.GET_COMMENT_TO_ME, mParams, CommentToEntity.class);
    }

    private void getFromMe() {
        initParameter();
        mParams.put("userId", MySharedPreferencesMgr.getString(SpfUtils.USER_ID, ""));
        mParams.put("token", MySharedPreferencesMgr.getString(SpfUtils.USER_TOKEN, ""));
        mParams.put("plat", "android");
        mParams.put("ifa", BaseApp.getDeviceID(mAct));
        mParams.put("page", page);
        mParams.put("pageSize", 10);

        sendEncryptPostRequest(AsRes.getInstance().getUrl(AsRes.GET_COMMENT_FROM_ME),
                AsRes.GET_COMMENT_FROM_ME, mParams, CommentFromEntity.class);
    }

    @Override
    protected void onSuccess(String content, BaseHttpClient client, Object parse) {
        mPullLayout.loadMoreFinish(true);
        mPullLayout.refreshFinish(true);
        if (client.getUrlIdentifier() == AsRes.GET_COMMENT_TO_ME) {
            CommentToEntity entity = (CommentToEntity) parse;
            if (ToolUtils.isSuccess(entity.getStatus())) {
                if (entity.getData() != null && entity.getData().size() > 0) {
                    if (page == 1) {
                        mToMeAdapter.setData(entity.getData());
                    } else {
                        mToMeAdapter.addDat(entity.getData());
                    }
                    page++;
                }
            } else if (entity.getStatus() == 10005) {
                MySharedPreferencesMgr.setString(SpfUtils.USER_ID, "");
                MySharedPreferencesMgr.setString(SpfUtils.USER_TOKEN, "");
                OAuthUtils.getInstance().cleanUserInfo();

                showToast("登录信息已过期，请重新登录");
                Intent intent = new Intent(mAct, LoginActivity.class);
                startActivity(intent);
            }
            ((CommentActivity) mAct).setTitleTv(mToMeAdapter.getCount());
        } else if (client.getUrlIdentifier() == AsRes.GET_COMMENT_FROM_ME) {
            CommentFromEntity entity = (CommentFromEntity) parse;
            if (ToolUtils.isSuccess(entity.getStatus())) {
                if (entity.getData() != null && entity.getData().size() > 0) {
                    if (page == 1) {
                        mFromMeAdapter.setData(entity.getData());
                    } else {
                        mFromMeAdapter.addDat(entity.getData());
                    }
                    page++;
                }
            } else if (entity.getStatus() == 10005) {
                MySharedPreferencesMgr.setString(SpfUtils.USER_ID, "");
                MySharedPreferencesMgr.setString(SpfUtils.USER_TOKEN, "");
                OAuthUtils.getInstance().cleanUserInfo();

                showToast("登录信息已过期，请重新登录");
                Intent intent = new Intent(mAct, LoginActivity.class);
                startActivity(intent);
            }
        }
    }

    @Override
    public void onRefresh(PullToRefreshLayout pullToRefreshLayout) {
        page = 1;
        upData();
    }

    @Override
    public void onLoadMore(PullToRefreshLayout pullToRefreshLayout) {
        upData();
    }
}